//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FlatExtrapolator2D.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Interpolations/Interpolation2D.h>
#include <gen/QL/Math/Matrix.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (Cephei::QL::Math::Interpolations::IInterpolation2D^ decoratedInterpolation) : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
    CInterpolation2D^ _CdecoratedInterpolation;
    try
    {
#ifdef HANDLE
        _phFlatExtrapolator2D = NULL;
#endif
        _CdecoratedInterpolation = safe_cast<CInterpolation2D^> (decoratedInterpolation);
        _CdecoratedInterpolation->Lock();
        boost::shared_ptr<QuantLib::Interpolation2D> _decoratedInterpolation = static_cast<boost::shared_ptr<QuantLib::Interpolation2D>> (_CdecoratedInterpolation->GetShared ()); 
        _ppFlatExtrapolator2D = new boost::shared_ptr<QuantLib::FlatExtrapolator2D> (new QuantLib::FlatExtrapolator2D ( _decoratedInterpolation ));
        SetInterpolation2D (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppFlatExtrapolator2D));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CdecoratedInterpolation != nullptr) _CdecoratedInterpolation->Unlock();
    }
}
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (boost::shared_ptr<QuantLib::FlatExtrapolator2D>& childNative, Object^ owner) : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
#ifdef HANDLE
	_phFlatExtrapolator2D = NULL;
#endif
	_ppFlatExtrapolator2D = &childNative;
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppFlatExtrapolator2D));
}
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (QuantLib::FlatExtrapolator2D& childNative, Object^ owner) : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
#ifdef HANDLE
	_phFlatExtrapolator2D = NULL;
#endif
	_ppFlatExtrapolator2D = new boost::shared_ptr<QuantLib::FlatExtrapolator2D> (&childNative);
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppFlatExtrapolator2D));
    _FlatExtrapolator2DOwner = owner;
    _Interpolation2DOwner = owner;
}

Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (CFlatExtrapolator2D^ copy) : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
#ifdef HANDLE
	_phFlatExtrapolator2D = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFlatExtrapolator2D = new boost::shared_ptr<QuantLib::FlatExtrapolator2D> (copy->GetShared());
        _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppFlatExtrapolator2D));
    }
}
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (PLATFORM::Type^ t) : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
#ifdef HANDLE
	_phFlatExtrapolator2D = NULL;
#endif
	if (!t->IsSubclassOf(CFlatExtrapolator2D::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (QuantLib::Handle<QuantLib::FlatExtrapolator2D>& childNative, Object^ owner)  : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
	_phFlatExtrapolator2D = &childNative;
	_ppFlatExtrapolator2D = &static_cast<boost::shared_ptr<QuantLib::FlatExtrapolator2D>>(childNative.currentLink());
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppFlatExtrapolator2D));
    _FlatExtrapolator2DOwner = owner;
}
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (QuantLib::Handle<QuantLib::FlatExtrapolator2D> childNative)  : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
	_phFlatExtrapolator2D = &childNative;
	_ppFlatExtrapolator2D = &static_cast<boost::shared_ptr<QuantLib::FlatExtrapolator2D>>(childNative.currentLink());
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppFlatExtrapolator2D));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::CFlatExtrapolator2D (QuantLib::FlatExtrapolator2D childNative)  : CInterpolation2D(CFlatExtrapolator2D::typeid)
{
#ifdef HANDLE
	_phFlatExtrapolator2D = NULL;
#endif
	_ppFlatExtrapolator2D = new boost::shared_ptr<QuantLib::FlatExtrapolator2D> (new QuantLib::FlatExtrapolator2D (childNative));
    _ppInterpolation2D = new boost::shared_ptr<QuantLib::Interpolation2D> (boost::dynamic_pointer_cast<QuantLib::Interpolation2D> (*_ppFlatExtrapolator2D));
}
#endif

Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::~CFlatExtrapolator2D ()
{
    if (_ppFlatExtrapolator2D != NULL)
    {
	    delete _ppFlatExtrapolator2D;
        _ppFlatExtrapolator2D = NULL;
    }
}
Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::!CFlatExtrapolator2D ()
{
    if (_ppFlatExtrapolator2D != NULL)
    {
	    delete _ppFlatExtrapolator2D;
    }
}
QuantLib::FlatExtrapolator2D& Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::GetReference ()
{
    if (_ppFlatExtrapolator2D == NULL) throw REFNEW NativeNullException ();
	return **_ppFlatExtrapolator2D;
}
boost::shared_ptr<QuantLib::FlatExtrapolator2D>& Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::GetShared ()
{
    if (_ppFlatExtrapolator2D == NULL) throw REFNEW NativeNullException ();
	return *_ppFlatExtrapolator2D;
}
QuantLib::FlatExtrapolator2D* Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::GetPointer ()
{
    if (_ppFlatExtrapolator2D == NULL) throw REFNEW NativeNullException ();
	return &**_ppFlatExtrapolator2D;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FlatExtrapolator2D>& Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::GetHandle ()
{
	if (_phFlatExtrapolator2D == NULL)
	{
		_phFlatExtrapolator2D = new Handle<QuantLib::FlatExtrapolator2D> (*_ppFlatExtrapolator2D);
	}
	return *_phFlatExtrapolator2D;
}
#endif
bool Cephei::QL::Math::Interpolations::CFlatExtrapolator2D::HasNative () 
{
	return (_ppFlatExtrapolator2D != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Interpolations::IFlatExtrapolator2D^ Cephei::QL::Math::Interpolations::CFlatExtrapolator2D_Factory::Create (Cephei::QL::Math::Interpolations::IInterpolation2D^ decoratedInterpolation)
{
    return REFNEW CFlatExtrapolator2D ( decoratedInterpolation);
}
